﻿using UnityEngine.UIElements;
using GraphProcessor;
using UnityEditor;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class PrefabToolbarView : ToolbarView
    {
        public PrefabToolbarView(BaseGraphView graphView) : base(graphView)
        {
        }

        protected override void AddButtons()
        {
            AddButton("Center", graphView.ResetPositionAndZoom);
            
            bool conditionalProcessorVisible = graphView.GetPinnedElementStatus<PrefabProcessorView>() != DropdownMenuAction.Status.Hidden;
            AddToggle("Show Prefab Processor", conditionalProcessorVisible, (v) => graphView.ToggleView<PrefabProcessorView>());
           
            AddButton("Show In Project", () => EditorGUIUtility.PingObject(graphView.graph), false);
            
            graphView.ClosePinned<LevelServiceProcessorView>();
            graphView.ClosePinned<SkillEditorProcessorView>();
        }
    }

    public class PrefabProcessorView : PinnedElementView
    {
        PrefabProcessor processor;

        public PrefabProcessorView() => title = "Prefab Processor";

        protected override void Initialize(BaseGraphView graphView)
        {
            processor = new PrefabProcessor(graphView.graph);

            graphView.computeOrderUpdated += processor.UpdateComputeOrder;

            Button runButton = new Button(OnPlay) { name = "ActionButton", text = "导出预设" };

            content.Add(runButton);
        }

        void OnPlay()
        {
            SkillEditorBlackboard.Instance.Clear();
            LevelServiceBlackboard.Instance.Clear();
            processor.Run();
        }

    }
}